package com.micro.glszh.stat.utils;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.ss.util.RegionUtil;

import java.util.HashMap;
import java.util.Objects;

/**
 * PoiExcelUtil 工具类
 *
 * @Author gaopengfei
 * @create 2025/2/12 10:33
 */
public class PoiExcelUtil {

    private static HashMap<Workbook, CellStyle> cellStyleHashMap = new HashMap<>(1);


    public static void setRangeCellStyle(CellRangeAddress cra, Sheet sheet) {
        // 使用RegionUtil类为合并后的单元格添加边框
        RegionUtil.setBorderBottom(BorderStyle.THIN, cra, sheet);
        RegionUtil.setBorderLeft(BorderStyle.THIN, cra, sheet);
        RegionUtil.setBorderRight(BorderStyle.THIN, cra, sheet);
        RegionUtil.setBorderTop(BorderStyle.THIN, cra, sheet);
    }

    public static CellStyle createHeaderStyle(Workbook workbook, HorizontalAlignment alignment) {
        CellStyle style = workbook.createCellStyle();
        Font font = workbook.createFont();
        font.setBold(true);
        style.setFont(font);
        style.setAlignment(alignment);
        style.setVerticalAlignment(VerticalAlignment.CENTER);
        style.setBorderBottom(BorderStyle.THIN);
        style.setBorderTop(BorderStyle.THIN);
        style.setBorderLeft(BorderStyle.THIN);
        style.setBorderRight(BorderStyle.THIN);
        return style;
    }

    public static CellStyle createDataStyle(Workbook workbook) {
        CellStyle style = cellStyleHashMap.get(workbook);
        if (Objects.nonNull(style)) {
            return style;
        }
        style = workbook.createCellStyle();
        style.setAlignment(HorizontalAlignment.CENTER);
        style.setVerticalAlignment(VerticalAlignment.CENTER);
        style.setBorderBottom(BorderStyle.THIN);
        style.setBorderTop(BorderStyle.THIN);
        style.setBorderLeft(BorderStyle.THIN);
        style.setBorderRight(BorderStyle.THIN);
        cellStyleHashMap.clear();
        cellStyleHashMap.put(workbook, style);
        return style;
    }

    public static void createCell(Row row, int column, String value, CellStyle style) {
        Cell cell = row.createCell(column);
        cell.setCellValue(value);
        cell.setCellStyle(style);
    }

    /**
     * 创建带边框的通用样式（可选）
     */
    private static CellStyle createBorderStyle(Workbook workbook) {
        CellStyle style = workbook.createCellStyle();
        style.setAlignment(HorizontalAlignment.CENTER);
        style.setVerticalAlignment(VerticalAlignment.CENTER);
        style.setBorderTop(BorderStyle.THIN);
        style.setBorderBottom(BorderStyle.THIN);
        style.setBorderLeft(BorderStyle.THIN);
        style.setBorderRight(BorderStyle.THIN);
        return style;
    }

    public static void applyBordersToUsedCells(HSSFSheet sheet, HSSFWorkbook workbook) {
        // 创建通用边框样式
        CellStyle borderStyle = createBorderStyle(workbook);

        // 遍历所有物理行
        for (int r = 0; r <= sheet.getLastRowNum(); r++) {
            HSSFRow row = sheet.getRow(r);
            if (row == null) continue;

            // 遍历所有物理单元格
            for (int c = 0; c <= row.getLastCellNum(); c++) {
                HSSFCell cell = row.getCell(c);
                if (cell == null) continue;

                // 处理现有样式（保留原有样式基础上添加边框）
                CellStyle originalStyle = cell.getCellStyle();
                CellStyle newStyle = workbook.createCellStyle();

                // 克隆原有样式
                newStyle.cloneStyleFrom(originalStyle);

                // 设置边框
                newStyle.setBorderTop(BorderStyle.THIN);
                newStyle.setBorderBottom(BorderStyle.THIN);
                newStyle.setBorderLeft(BorderStyle.THIN);
                newStyle.setBorderRight(BorderStyle.THIN);

                // 应用新样式
                cell.setCellStyle(newStyle);
            }
        }
    }
}
